package storageManager;

import java.util.ArrayList;

public class IndexRecord extends Record implements Comparable<IndexRecord>{
	private String keyValue;
	private byte pageNumber;
	private byte recordNumber;
	public IndexRecord(){
		
	}	
	public IndexRecord(String keyValue, ArrayList<Integer> placeOfDataRecord, TableRecord tableRecord){
		this.keyValue = keyValue;
		this.pageNumber = placeOfDataRecord.get(0).byteValue();
		this.recordNumber = placeOfDataRecord.get(1).byteValue();
	}
	public String getKeyValue() {
		return keyValue;
	}
	public void setKeyValue(String keyValue) {
		this.keyValue = keyValue;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = (byte)pageNumber;
	}
	public int getRecordNumber() {
		return recordNumber;
	}
	public void setRecordNumber(int recordNumber) {
		this.recordNumber = (byte)recordNumber;
	}
	@Override
	public int compareTo(IndexRecord o) {//This method overrides the implemented interface's method. This helps me to use Collections.sort method for index records
		return Tools.comparator(this.keyValue, o.getKeyValue());
	}
	
	public byte[] toByte(int sizeOfTheKeyValue){//Parse the index record object to a byte array
		byte[] result = new byte[sizeOfTheKeyValue+2];
		System.arraycopy(Tools.stringToByte(keyValue,sizeOfTheKeyValue), 0, result, 0, sizeOfTheKeyValue);
		result[sizeOfTheKeyValue] = (byte) pageNumber;
		result[sizeOfTheKeyValue+1] = (byte) recordNumber;
		return result;
	}
}
